﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmThemPhieuNhapKho : Form
    {
        public frmThemPhieuNhapKho()
        {
            InitializeComponent();
        }
        public List<NguyenVatLieu_SanPhamDTO> listNVL;

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int id = dgvChiTiet.CurrentCell.RowIndex;
            dgvChiTiet.Rows.RemoveAt(id);
        }

        private void btnQuayRa_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void LoadComboboxNhanVienLap()
        {
            try
            {
                NhanVienBUS nvBUS = new NhanVienBUS();
                List<NhanVienDTO> listNV = new List<NhanVienDTO>();
                listNV = nvBUS.LayDanhSachNhanVien();
                this.cbNhanVienLap.DataSource = listNV;
                this.cbNhanVienLap.DisplayMember = "TenNV";
                cbNhanVienLap.ValueMember = "MaNV";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load combobox!" + ex.Message);
            }
        }

        public void LoadComboboxNguyenVatLieu()
        {
            try
            {
                NguyenVatLieuBUS nvlBUS = new NguyenVatLieuBUS();
                listNVL = new List<NguyenVatLieu_SanPhamDTO>();
                listNVL = nvlBUS.LayDanhSachNguyenVatLieu();
                this.cbTenNVL.DataSource = listNVL;
                this.cbTenNVL.DisplayMember = "Ten";
                cbTenNVL.ValueMember = "MaNVL";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load combobox!" + ex.Message);
            }
        }

        private void frmThemPhieuNhapKho_Load(object sender, EventArgs e)
        {
            LoadComboboxNhanVienLap();
            LoadComboboxNguyenVatLieu();
        }

        void frm_KhiLayMa(DonDH_NguyenVatLieuDTO ddh)
        {
            txtMaDDH.Text = Convert.ToString(ddh.MaDDH);
        }

        private void btnLayMa_Click(object sender, EventArgs e)
        {
            frmDonDatHangNVL fr = new frmDonDatHangNVL();
            fr.KhiLayMa += frm_KhiLayMa;
            fr.ShowDialog();
        }

        private void dgvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvChiTiet.CurrentCell.ColumnIndex == 1)
            {
                ComboBox cbx = e.Control as ComboBox;
                if (cbx != null)
                {
                    cbx.SelectedIndexChanged -= comboBox_SelectedIndexChanged;
                    cbx.SelectedIndexChanged += comboBox_SelectedIndexChanged;
                }
            }
        }
        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int manguyenvatlieu = Convert.ToInt32(((ComboBox)sender).SelectedValue);
                foreach (NguyenVatLieu_SanPhamDTO nvl in listNVL)
                {
                    if (nvl.MaNVL == manguyenvatlieu)
                    {
                        Double giaban = nvl.DonGiaBan;
                        string donvi = nvl.DonViTinh;
                        int manvl = nvl.MaNVL;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[0].Value = manvl.ToString();
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[3].Value = donvi;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[4].Value = giaban;

                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void dgvChiTiet_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                try
                {
                    if (dgvChiTiet.Rows[e.RowIndex].Cells[1].Value.ToString() != "" && dgvChiTiet.Rows[e.RowIndex].Cells[4].Value.ToString() != "")
                    {
                        int soluong = Convert.ToInt32(dgvChiTiet.Rows[e.RowIndex].Cells[2].Value.ToString());
                        double dongia = Convert.ToDouble(dgvChiTiet.Rows[e.RowIndex].Cells[4].Value.ToString());
                        double thanhtien = soluong * dongia;
                        dgvChiTiet.Rows[e.RowIndex].Cells[5].Value = thanhtien.ToString();
                    }
                }

                catch (Exception)
                {
                }
            }
            //Tính tổng tiền
            double tongtien = 0;
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                try
                {
                    if (dgvChiTiet.Rows[i].Cells[5].Value != "")
                    {
                        double thanhtien = Convert.ToDouble(dgvChiTiet.Rows[i].Cells[5].Value);
                        tongtien += thanhtien;
                    }
                }
                catch (Exception)
                {
                }
            }
            lbTongTien.Text = tongtien.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                int kq = -1;
                string themct = "";
                PhieuNhapKhoBUS pnkBUS = new PhieuNhapKhoBUS();
                PhieuNhapKhoDTO pnkDTO = new PhieuNhapKhoDTO();
                pnkDTO.NgayLap = DateTime.Parse(this.dtpNgayLap.Text);
                pnkDTO.MaNV = Convert.ToInt32(cbNhanVienLap.SelectedValue.ToString());
                pnkDTO.MaDDH = Convert.ToInt32(txtMaDDH.Text);
                pnkDTO.TongTien = Convert.ToDouble(lbTongTien.Text);

                kq = pnkBUS.ThemPhieuNhapKho(pnkDTO);

                //them chi tiet
                CT_PhieuNhapKhoBUS ctpnkBUS = new CT_PhieuNhapKhoBUS();
                for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
                {
                    try
                    {
                        CT_PhieuNhapKhoDTO ctpnkDTO = new CT_PhieuNhapKhoDTO();
                        ctpnkDTO.MaPhieuNK = kq;
                        ctpnkDTO.MaNVL = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[0].Value);
                        ctpnkDTO.SoLuong = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[2].Value);
                        ctpnkDTO.DonGiaNhap = Convert.ToDouble(dgvChiTiet.Rows[i].Cells[4].Value);
                        if (ctpnkDTO.MaNVL != -1)
                        {
                            themct = ctpnkBUS.ThemChiTietPhieuNhapKho(ctpnkDTO);
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Thêm ct phiếu nhập kho không thành công!");
                    }
                }
                if (kq != -1 && themct != "")
                {
                    MessageBox.Show("Thêm phiếu nhập kho thành công!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Thêm phiếu nhập kho không thành công!" + ex.Message);
            }
        }
    }
}
